﻿Imports MindFusion.Scheduling
Imports MindFusion.Scheduling.WinForms
Imports System.Drawing.Drawing2D

Public Class FrmStage

    Private szSQL As String
    Private rs As ADODB.Recordset
    Dim szGroupe As String
    Private centered As StringFormat
    Private cleft As StringFormat
    Dim debutStage As Date

    Private Sub FrmStage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.Text = LibelleStage(gTypePlanning)

        ' définir l'orientation du texte
        centered = New StringFormat
        centered.Alignment = StringAlignment.Center
        centered.LineAlignment = StringAlignment.Center
        cleft = New StringFormat
        cleft.Alignment = StringAlignment.Near
        cleft.LineAlignment = StringAlignment.Center

        ' borner le calendrier à afficher
        CAL_STAGES.Date = getLundiPrecedent(Now.Date)
        CAL_STAGES.EndDate = CAL_STAGES.Date.AddDays(34)
        ' déterminer le numéro de semaine
        Date2Semaine(CAL_STAGES.Date)

        Dim resource As Resource = Nothing
        ' créer les groupes A, B, C, ...
        CAL_STAGES.Resources.Clear()
        For i As Integer = 1 To 7
            resource = New Resource
            resource.Name = "Groupe " & Chr(64 + i)
            resource.Id = Chr(64 + i)
            CAL_STAGES.Resources.Add(resource)
        Next
        resource = Nothing

        ' afficher sur 5 semaines
        InitListeStages()

    End Sub

    Sub InitListeStages()
        Dim dDate As Date
        Dim nGroupe As Integer = 0
        Dim noSemaine As Integer
        Dim app As Appointment

        Me.Cursor = Cursors.WaitCursor
        ' effacer le calendrier
        CAL_STAGES.BeginInit()
        CAL_STAGES.Schedule.Clear()

        ' Lire les 5 semaines à partir de dateDebut
        dDate = CAL_STAGES.Date
        For i = 1 To 5
            noSemaine = (dDate.Year * 100) + DatePart(DateInterval.WeekOfYear, dDate, , FirstWeekOfYear.Jan1)
            ' Lire la table des planning stage
            szSQL = "CALL spGet_PLA_STAGE('" & gTypePlanning & "'," & noSemaine & ",'')"
            GetRecordset(ctDSN_AEM, szSQL, rs)

            Do While Not rs.EOF
                app = New Appointment()
                app.StartTime = rs.Fields("debut").Value
                app.EndTime = rs.Fields("fin").Value
                app.EndTime = app.EndTime.AddDays(1)
                app.HeaderText = nv(rs.Fields("libelle").Value) & vbNewLine & _
                                 rs.Fields("dispo").Value & " / " & nv(rs.Fields("total").Value)
                nGroupe = Asc(rs.Fields("groupe").Value) - 65
                app.Resources.Add(CAL_STAGES.Resources(nGroupe))
                app.Id = rs.Fields("semaine").Value.ToString & rs.Fields("groupe").Value
                app.Tag = nv(rs.Fields("valide").Value)
                If app.Tag = "O" Then
                    ' stage déjà validé
                    app.Style.HeaderBrush = New MindFusion.Drawing.SolidBrush(Color.LightGreen)
                ElseIf StageHasPending(gTypePlanning, noSemaine, rs.Fields("groupe").Value) = True Then
                    ' stage a des inscrits provisoires
                    app.Style.HeaderBrush = New MindFusion.Drawing.SolidBrush(Color.LightPink)
                    app.HeaderText &= " (en attente)"
                ElseIf rs.Fields("dispo").Value = 0 Then
                    ' stage complet 
                    app.Style.HeaderBrush = New MindFusion.Drawing.SolidBrush(Color.LightSteelBlue)
                    app.HeaderText &= " (complet)"
                End If
                app.AllowChangeEnd = True
                app.AllowChangeStart = True
                app.AllowMove = False
                app.AllDayEvent = True
                CAL_STAGES.Schedule.Items.Add(app)
                rs.MoveNext()
            Loop

            ' semaine suivante
            dDate = dDate.AddDays(7)
            ' Close the recordset AND connection.
            rs.Close()
        Next
        CAL_STAGES.EndInit()
        Me.Cursor = Cursors.Default

    End Sub

    Private Sub CAL_STAGES_DateClick(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.ResourceDateEventArgs) Handles CAL_STAGES.DateClick

        ' date de debut du stage
        debutStage = getLundiPrecedent(e.Date)
        ' déterminer le numéro de semaine
        Date2Semaine(debutStage)

        ' double-click ? oui, insérer nouveau groupe
        If e.Clicks = 2 And e.Button = System.Windows.Forms.MouseButtons.Left Then
            If IsUserAuthorised(9) = False Then
                Exit Sub
            End If
            szSQL = "CALL spINS_PLA_STAGE('" & gTypePlanning & "'," & TE_ANNEE.Text & TE_SEMAINE.Text & ",'" & e.Resource.Id & "','" & _
                    Format(debutStage, "yyyy/MM/dd") & "','" & Format(debutStage.AddDays(5), "yyyy/MM/dd") & "'," & gUserAgence & ",'" & gUserCode & "')"
            If ExecQuery(ctDSN_AEM, szSQL) Then
                ' réaffiche le calendrier
                InitListeStages()
            End If
        End If

    End Sub

    Private Sub cal_STAGES_ItemClick(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.ItemMouseEventArgs) Handles CAL_STAGES.ItemClick

        If System.Windows.Forms.MouseButtons.Left Then

            ' date de debut du stage
            debutStage = e.Item.StartTime
            ' déterminer le numéro de semaine
            Date2Semaine(debutStage)

            ' double-click = Modifier
            If e.Clicks = 2 Then
                ' afficher les détails de la ligne NoLigne
                gSemaine = CInt(Mid(e.Item.Id, 1, 6))
                gGroupe = Mid(e.Item.Id, 7, 1)
                FrmStageMAJ.ShowDialog()
                ' réaffiche le planning
                InitListeStages()
            End If
        End If
    End Sub

    Private Sub BO_PREC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_PREC.Click

        ' reculer le calendrier de 5 semaines
        CAL_STAGES.EndDate = CAL_STAGES.Date.AddDays(-1)
        CAL_STAGES.Date = CAL_STAGES.EndDate.AddDays(-34)
        ' déterminer le numéro de semaine
        Date2Semaine(CAL_STAGES.Date)
        ' afficher sur 5 semaines
        InitListeStages()
        ' utilisateur autorisé à valider ?

    End Sub

    Private Sub BO_SUIVANT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_SUIVANT.Click

        ' avancer le calendrier de 5 semaines
        CAL_STAGES.Date = CAL_STAGES.EndDate.AddDays(1)
        CAL_STAGES.EndDate = CAL_STAGES.Date.AddDays(34)
        ' déterminer le numéro de semaine
        Date2Semaine(CAL_STAGES.Date)
        ' afficher sur 5 semaines
        InitListeStages()
        ' utilisateur autorisé à valider ?

    End Sub

    Private Sub cal_STAGES_Draw(ByVal sender As Object, ByVal e As MindFusion.Scheduling.WinForms.DrawEventArgs) Handles CAL_STAGES.Draw

        Dim bounds As Rectangle = e.Bounds
        Dim g As MindFusion.Drawing.IGraphics = e.Graphics
        Dim Jour As Date = e.Date

        Select Case e.Element
            Case CustomDrawElements.ResourceViewRowHeader
                ' encadrer les nom des ressources
                Dim r As Resource = e.Resource

                g.FillRectangle(Brushes.White, e.Bounds)
                g.DrawString(r.Name, e.Style.HeaderFont, Brushes.Navy, _
                             New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), centered)
                g.DrawLine(Pens.Gray, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right - 1, e.Bounds.Bottom - 1)
                ' tracer une ligne sur bordure gauche
                g.DrawLine(Pens.Gray, e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom)

            Case CustomDrawElements.ResourceViewTimelineCell

                '           Dim b As Rectangle = e.Bounds
                Dim text As String = e.Text
                Dim isWeek As Boolean = False

                ' texte de l'entête = Semaine nn
                If text = "Week" Then
                    isWeek = True
                    Dim week As Integer = DatePart(DateInterval.WeekOfYear, e.Date, , FirstWeekOfYear.Jan1)
                    text = String.Format("semaine {0}", week)
                End If
                ' colorier l'entête
                Dim bbrush As Brush = Brushes.White
                If e.Date = DateTime.Now.Date Then
                    bbrush = Brushes.Orange
                ElseIf e.Date.DayOfWeek = CAL_STAGES.FirstWeekendDay Then
                    bbrush = Brushes.LightGray
                ElseIf IsHoliday(e.Date) Then
                    bbrush = Brushes.LightSalmon
                End If
                g.FillRectangle(bbrush, bounds)
                ' tracer une ligne sur bordure gauche
                g.DrawLine(Pens.Gray, e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom)

                Dim textBrush As Brush = New SolidBrush(e.Style.HeaderTextColor)
                Dim textDest As Rectangle = bounds
                Dim state As GraphicsState = Nothing

                ' Rotation du texte du sous-titre
                If Not isWeek Then
                    textDest = New Rectangle(bounds.X, bounds.Y, bounds.Height, bounds.Width)
                    state = e.Graphics.Save()

                    Dim off As Single = 0
                    If textDest.Width > textDest.Height Then
                        off = CType(Math.Max(textDest.Width, textDest.Height) / 2, Single)
                    Else
                        off = CType(Math.Min(textDest.Width, textDest.Height) / 2, Single)
                    End If
                    g.TranslateTransform(textDest.Left + off, textDest.Top + off)
                    g.RotateTransform(270)
                    g.TranslateTransform(-textDest.Left - off, -textDest.Top - off)
                End If

                Dim f As StringFormat
                If isWeek Then f = cleft Else f = centered

                g.DrawString(text, e.Style.HeaderFont, textBrush, _
                New RectangleF(textDest.X, textDest.Y, textDest.Width, textDest.Height), f)

                If Not isWeek Then
                    g.Restore(state)
                End If

                textBrush.Dispose()

            Case CustomDrawElements.ResourceViewCell
                ' colorier la colonne
                Dim bcolor As Brush = Brushes.White
                If Jour = DateTime.Now.Date Then
                    bcolor = Brushes.Orange
                ElseIf Jour.DayOfWeek = CAL_STAGES.FirstWeekendDay Then
                    bcolor = Brushes.LightGray
                ElseIf IsHoliday(Jour) Then
                    bcolor = Brushes.LightSalmon
                End If
                g.FillRectangle(bcolor, bounds)
                ' tracer une ligne sur bordure gauche
                g.DrawLine(Pens.Gray, e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom)

        End Select

    End Sub

    Private Sub Date2Semaine(ByVal _date As Date)
        ' déterminer le numéro de semaine
        TE_SEMAINE.Text = Format(DatePart(DateInterval.WeekOfYear, _date, , FirstWeekOfYear.Jan1), "00")
        TE_ANNEE.Text = _date.Year
    End Sub

    Private Sub CAL_STAGES_ItemDeleting(sender As Object, e As MindFusion.Scheduling.WinForms.ItemConfirmEventArgs) Handles CAL_STAGES.ItemDeleting
        ' interdire la suppression
        e.Confirm = False
    End Sub
End Class

